<?php


namespace app\common;
use app\common\service\JwtService;

trait RootAuth
{
    /**
     * 配发后台登录Token
     * @param int $userId
     * @param int $minute Token过期时间（分钟）
     * @return \Lcobucci\JWT\Token\Plain
     */
    public function issueAdminToken(int $userId)
    {
        return (new JwtService())->issueAdminToken($userId);
    }

    /**
     * 配发APP登录Token
     * @param int $userId
     * @return \Lcobucci\JWT\Token\Plain
     */
    public function issueApiToken(int $userId)
    {
        return (new JwtService())->issueApiToken($userId);
    }

    /**
     * Token校验
     * @param string|null $accessToken
     * @return bool
     */
    public function checkToken(string $accessToken = null)
    {
        $jwtToken = empty($accessToken) ? '' : cache($accessToken);
        return empty($jwtToken) ? false : (new JwtService())->verifyToken($jwtToken);
    }
}